package com.yxy.f_music_server.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yxy.f_music_server.common.PageResult;
import com.yxy.f_music_server.constant.MessageConstant;
import com.yxy.f_music_server.domain.dto.SongPageQueryDTO;
import com.yxy.f_music_server.domain.po.Song;
import com.yxy.f_music_server.domain.vo.SongPageQueryVO;
import com.yxy.f_music_server.exception.MusicAlreadyExistsException;
import com.yxy.f_music_server.mapper.MusicMapper;
import com.yxy.f_music_server.service.MusicService;
import com.yxy.f_music_server.utils.QiniuOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Service
public class MusicServiceImpl implements MusicService {

    @Autowired
    private MusicMapper musicMapper;

    /**
     * 添加音乐
     * @param song
     */
    @Override
    @Transactional
    public void add(Song song) {
        musicMapper.add(song);
    }

    /**
     * 获取音乐列表
     * @param songPageQueryDTO
     * @return
     */
    @Override
    public PageResult getMusicList(SongPageQueryDTO songPageQueryDTO) {
        PageHelper.startPage(songPageQueryDTO.getPage(), songPageQueryDTO.getPageSize());
        Page<SongPageQueryVO> page = musicMapper.getMusicList(songPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据标题查询
     * @param title
     * @return
     */
    @Override
    public Song getByTitle(String title) {
        // 判断是否存在
        Song s = musicMapper.getByTitle(title);
        if(s != null){
            throw new MusicAlreadyExistsException(MessageConstant.MUSIC_ALREADY_EXISTS);
        }
        return null;
    }
}
